package com.bankcomm.ui.specialwidget;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

import com.bankcomm.R;
import com.bankcomm.ui.adapter.ArrayWheelAdapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

/**
 * 下拉框
 * 
 * @author zhang.h
 * 
 * @date 2012-6-29 下午03:17:49
 */
public class MySelect extends EditText {

	Context context_;
	PopupWindow popupWindow;
	ListView list;
	LinkedHashMap<String, String> typeMap_;

	public MySelect(Context context) {
		super(context);
	}

	public MySelect(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MySelect(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		switch (action) {
		case KeyEvent.ACTION_DOWN:
			this.requestFocus();
			// setTextColor(R.color.gray);
			break;
		case KeyEvent.ACTION_UP:
			mCertDialog.show();
			break;
		default:
			return super.onTouchEvent(event);
		}
		return true;
	}

	public void setType(LinkedHashMap<String, String> typeMap) {
		typeMap_ = typeMap;

		// 遍历typeMap
		int index = 0;
		String[] type = new String[typeMap.size()];
		Iterator<Entry<String, String>> iter = typeMap.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, String> entry = iter.next();
			String key = entry.getKey();
			// 设置第一个为默认值
			if (index == 0) {
				setText(key);
				setValue(entry.getValue());
			}
			type[index] = key;
			index++;
		}
	}

	private String value;
	private Dialog mCertDialog;

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	int index = 0;

	public void initWeelSelect(LinkedHashMap<String, String> typeMap) {
		typeMap_ = typeMap;

		// 遍历typeMap
		final String[] type = new String[typeMap.size()];
		Iterator<Entry<String, String>> iter = typeMap.entrySet().iterator();
		while (iter.hasNext()) {
			Entry<String, String> entry = iter.next();
			String key = entry.getKey();
			// 设置第一个为默认值
			if (index == 0) {
				setText(key);
				setValue(entry.getValue());
			}
			type[index] = key;
			index++;
		}
		View view = LayoutInflater.from(getContext()).inflate(
				R.layout.layout_select_popup, null);
		LinearLayout parent = (LinearLayout) view
				.findViewById(R.id.wheelParent);
        LinearLayout layout=(LinearLayout)view.findViewById(R.id.buttonlayout);
        layout.setVisibility(View.GONE);
		WheelView mCertWheel = new WheelView((Activity) getContext());
		mCertWheel.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
		parent.addView(mCertWheel);
		mCertWheel.setAdapter(new ArrayWheelAdapter<String>(type));
 
		mCertDialog = new Dialog(getContext(), R.style.popupAnimation);
		mCertDialog.setContentView(view);
		mCertDialog.setCancelable(true);
		mCertDialog.setCanceledOnTouchOutside(true);

		Window mWindow = mCertDialog.getWindow();
		mWindow.setWindowAnimations(R.style.popupAnimation);
		mWindow.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL);
		mWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);

		mCertWheel.addScrollingListener(new OnWheelScrollListener() {
			@Override
			public void onScrollingStarted(WheelView wheel) {
			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				index = wheel.getCurrentItem();
				String typeName = type[index];
				setText(typeName);

				// 提交值
				setValue(typeMap_.get(typeName));
				System.err.println(typeMap_.get(typeName));
			}
		});
	}

}
